package BusinessLogic;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class CoursesContainer {

	List<Course> courses;
	List<Session> sessions;
	public CoursesContainer(List<Course> courses, List<Session> sessions) {
		super();
		this.courses = courses;
		this.sessions = sessions;
	}
	public List<Course> getCourses() {
		return courses;
	}
	public List<Session> getSessions() {
		return sessions;
	}
	public List<Session> getSessionsOfCourse(String course)
	{
		List<Session> sessions = new LinkedList<Session>();
		for (Session session : this.sessions)
			if (session.getCourse().equals(course))
				sessions.add(session);
		Collections.sort(sessions);
		return sessions;
	}
	public List<Session> getSessionsOfDay(String day)
	{
		List<Session> sessions = new LinkedList<Session>();
		for (Session session : this.sessions)
			if (session.getDay().equals(day))
				sessions.add(session);
		Collections.sort(sessions);
		return sessions;
	}
	public String getXML()
	{
	  String[][] table= new String[7][10];
	  for (int i=0 ; i<table.length ; i++)
		 for (int j=0 ; j< table[i].length ; j++)
			 table[i][j] = "";
	  for (Session s: sessions ){
	    int day = Session.days.get(s.getDay());
	    String course = s.getCourse();
	    for (int i=s.getStart_time() ; i< s.getEnd_time() ; ++i){
	      table[day-1][i-8]= course;
	    }
	  }
		String xml = "<!DOCTYPE timetable SYSTEM 'public/xsl.dtd'>";
		xml+="<timetable>";
		for(int i=0 ; i<10 ; ++i){
			xml+="<slot id='s"+i+"' hour='"+i+"'>";
		  for (int j=0 ; j<7 ; ++j){
		  	xml+="<day name='"+fromNumToDay(j)+"'>";
		    xml+=table[j][i];
		    xml+="<\\/day>";
		  }
		  xml+="<\\/slot>";
		}
		xml+="<\\/timetable>";
		return "\""+xml+"\"";
	}
	
	private String fromNumToDay(int num){
	  switch (num){
	    case 0: return "Sunday";
	    case 1: return "Monday";
	    case 2: return "Tuesday";
	    case 3: return "Wednesday";
	    case 4: return "Thursday";
	    case 5: return "Friday";
	    default : return "Saturday";
	  }
	}
}
